package org.gbif.metadata.eml;

import java.io.IOException;
import java.io.Serializable;

import org.jibx.runtime.BindingDirectory;
import org.jibx.runtime.IBindingFactory;
import org.jibx.runtime.IMarshallingContext;
import org.jibx.runtime.IUnmarshallingContext;

/**
 * Eml object that supports the GBIF profile model.
 * 
 * This includes a custom serializer.
 * 
 * @author davemartin
 */
public class Eml implements Serializable {
	
  protected Dataset dataset;

  protected String packageId;

  protected String system;

  /**
   * Custom serialization using JIBX.
   * 
   * @param out
   * @throws IOException
   */
  private void writeObject(java.io.ObjectOutputStream out) throws IOException{
	  try {	  
		 IBindingFactory bfact = BindingDirectory.getFactory(Eml.class);
		 IMarshallingContext uctx = bfact.createMarshallingContext();
		 uctx.marshalDocument(this, "UTF-8", true, out);
	 } catch(Exception e){
		 e.printStackTrace();
		 throw new IOException(e.getMessage());
	 }
  }

  /**
   * Custom deserialization using JIBX.
   * @param in
   * @throws IOException
   * @throws ClassNotFoundException
   */
  private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
	 try {
		 IBindingFactory bfact = BindingDirectory.getFactory(Eml.class);
		 IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
		 Eml eml = (Eml) uctx.unmarshalDocument(in, null);
		 this.packageId = eml.getPackageId();
		 this.system = eml.getSystem();
		 this.dataset = eml.getDataset();
	 } catch(Exception e){
		 e.printStackTrace();
		 throw new IOException(e.getMessage());
	 }
  }

  public Dataset getDataset() {
    return this.dataset;
  }

  public void setDataset(Dataset dataset) {
    this.dataset = dataset;
  }

  public String getPackageId() {
    return this.packageId;
  }

  public void setPackageId(String packageId) {
    this.packageId = packageId;
  }

  public String getSystem() {
    return this.system;
  }

  public void setSystem(String system) {
    this.system = system;
  }

}
